package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Region;
import cn.wolfcode.wolf2w.query.DestinationQuery;
import cn.wolfcode.wolf2w.repository.DestinationRepository;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IRegionService;
import cn.wolfcode.wolf2w.util.PageHelperUtil;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by 甘静斌 on 2020-08-07 13:41
 */
@Service
//@Transactional
public class DestinationServiceImpl implements IDestinationService {

    @Autowired
    private DestinationRepository repository;
    
    @Autowired
    private IRegionService regionService;

    @Autowired
    private MongoTemplate template;


    @Override
    public void save(Destination destination) {

        repository.save(destination);
    }

    @Override
    public void update(Destination destination) {

        repository.save(destination);
    }

    @Override
    public void delete(String id) {
        repository.deleteById(id);
    }

    @Override
    public Destination get(String id) {
        return repository.findById(id).orElse(null);
    }

    @Override
    public List<Destination> list() {
        return repository.findAll();
    }

    @Override
    public List<Destination> getDestByRegionId(String rid) {

        //获取区域对象
        Region region = regionService.get(rid);
        //通过区域对象获取管理目的地集合
        List<String> ids = region.getRefIds();

        //返回目的地集合
        return repository.findByIdIn(ids);
    }

    @Override
    public Page<Destination> query(DestinationQuery qo) {

        Query query = new Query();

        //total  pageable  list
        if (StringUtils.hasLength(qo.getParentId())){
            query.addCriteria(Criteria.where("parentId").is(qo.getParentId()));
        }else {
            query.addCriteria(Criteria.where("parentId").is(null));
        }
        if (StringUtils.hasLength(qo.getKeyword())){
            Criteria criteria = Criteria.where("name").regex(qo.getKeyword());
            query.addCriteria(criteria);
        }

        Pageable pageable = PageRequest.of(qo.getCurrentPage()-1,qo.getPageSize() ,Sort.Direction.ASC,"_id" );

        Page<Destination> page = PageHelperUtil.query(template, pageable, Destination.class, query, qo);
        return page;
    }
    //吐司查询
    @Override
    public List<Destination> findToasts(String parentId) {

        List<Destination> list = new ArrayList<>();

        //递归
        this.createToast(list,parentId);
        //list集合元素顺序调转
        Collections.reverse(list);

        return list;
    }

    @Override
    public List<Destination> queryDestByRegionId(String regionId) {

        List<Destination> list = null;
        //中国的id 为 "-1" 所以要做一个判断条件
        if ("-1".equals(regionId)){
            list = repository.findByparentName("中国");
        }else {
            list = this.getDestByRegionId(regionId);
        }
        //遍历目的地的集合,并进行一个分页数据,每个目的地显示几个
        for (Destination dest : list) {
            Pageable pageable = PageRequest.of(0,6 ,Sort.Direction.ASC,"_id" );

            List<Destination> children = repository.findByparentId(dest.getId(),pageable);
            dest.setChildren(children);
        }
        return list;
    }

    @Override
    public void updateInfo(String id,String info) {

        //部分字段更新
        Destination destination = get(id);
        destination.setInfo(info);
        save(destination);


    }

    @Override
    public List<Destination> list(long deep) {

       List<Destination> list = repository.findByDeep(deep);
        return list;
    }

    @Override
    public Destination queryByName(String keyword) {
        return repository.findByName(keyword);
    }

    //吐司查询操作使用递归
    private void createToast(List<Destination> list,String parentId){

        //递归前提:有一个出口
        if (!StringUtils.hasLength(parentId)){
            return;
        }

        Destination dest = this.get(parentId);
        list.add(dest);

        if (StringUtils.hasLength(dest.getParentId())){
            createToast(list,dest.getParentId() );
        }
    }


}
